package com.tarena.lbs.basic.web.controller;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.basic.web.service.AdminService;
import com.tarena.lbs.common.passport.encoder.JwtEncoder;
import com.tarena.lbs.common.passport.principle.UserPrinciple;
import com.tarena.lbs.pojo.basic.param.AdminParam;
import com.tarena.lbs.pojo.basic.query.AdminQuery;
import com.tarena.lbs.pojo.basic.vo.AdminVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class AdminController {
    @Autowired
    private JwtEncoder<UserPrinciple> jwtEncoder;
    @Autowired
    private AdminService adminService;
    //如果增删查改对应的某个表格 **PO持久层对象,会有一个几乎一样的VO视图对象
    //前端携带jwt 拿到解析
    @GetMapping("/admin/basic/role/detail")
    public Result<AdminVO> detail() throws BusinessException {
        Thread thread = Thread.currentThread();
        log.info("控制层执行的线程id:{}",thread.getId());
        log.info("控制层执行的线程对象:{}",thread);
        //在controller方法拿到请求头 Authorization
        /*String jwt = request.getHeader("Authorization");*/
        //解析jwt 拿到返回值 Integer userId
        /*Integer userId=parseJwt(jwt);*/
        AdminVO vo=adminService.detail();
        return new Result<>(vo);
    }

    @PostMapping("/admin/basic/role/list")
    public Result<PageResult<AdminVO>> pageList(@RequestBody AdminQuery query){
        return new Result<>(adminService.pageList(query));
    }
    //商家账号表单提交新增
    @PostMapping("/admin/basic/role/add")
    public Result<Void> addAdmin(@RequestBody AdminParam param)
        throws BusinessException{
        adminService.addAdmin(param);
        return Result.success();
    }

    /*private Integer parseJwt(String jwt) throws BusinessException {
        //判断能否拿到jwt 断言是空
        *//*if (jwt==null||jwt.length()==0){
            throw new BusinessException()
        }*//*
        Asserts.isTrue(jwt==null||jwt.length()==0,new BusinessException("-2","认证失败"));
        //解析jwt jwtEncoder
        UserPrinciple userPrinciple = jwtEncoder.getLoginFromToken(jwt, UserPrinciple.class);
        //正常解析成功
        log.info("用户详情查询拿到认证对象:{}",userPrinciple);
        return userPrinciple.getId();
    }*/
}
